package com.etc.controller;

import com.etc.service.CinemaService;
import com.etc.service.RecorddetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2019/5/21.
 */
@Controller
public class RecorddetailsController {
    @Autowired
    private RecorddetailsService recorddetailsService;
    @Autowired
    private CinemaService cinemaService;

    @GetMapping(value = "seat/{cid}")
    public String selectSeatByCid(@PathVariable(value = "cid") Integer cid, Model model, HttpServletResponse response, HttpSession session) {
       if(session.getAttribute("user")==null){
           return "redirect:/tellogin";
       }
        List<String> seats = recorddetailsService.findSeatidByCid(cid);
        model.addAttribute("seats", seats);
        model.addAttribute("price",cinemaService.getcinema(cid).getMprice());
        //把cid存到cookie

        Cookie cid_cookie = new Cookie("cid", String.valueOf(cid).trim());
        cid_cookie.setMaxAge(30 * 60);// 设置为30min
        cid_cookie.setPath("/");
        response.addCookie(cid_cookie);

        Cookie price_cookie = new Cookie("price", String.valueOf(cinemaService.getcinema(cid).getMprice()).trim());
        price_cookie.setMaxAge(30 * 60);// 设置为30min
        price_cookie.setPath("/");
        response.addCookie(price_cookie);

        return "seat";
    }
}
